package com.xnyzc.lhy.common.exception;


import com.xnyzc.lhy.common.entity.Sb;
import com.xnyzc.lhy.common.penum.coupon.CouponErrorCode;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;


/**
 * 优惠券异常
 */
@Slf4j
@Data
public class CouponException extends RuntimeException {

    /**
     * 定义枚举
     */
    private CouponErrorCode errno;

    /**
     * 问题描述
     */
    private String errmsg;

    /**
     * 构造函数
     *
     * @param errno
     */
    public CouponException(CouponErrorCode errno) {
        super(errno.toChString());

        this.errno = errno;
        this.errmsg = errno.toChString();
    }

    /**
     * 构造函数
     *
     * @param errno
     * @param errmsg
     */
    public CouponException(CouponErrorCode errno, String errmsg) {
        super(errmsg);

        this.errno = errno;
        this.errmsg = errmsg;
    }

    public CouponException(Exception e) {
        super(e);

        this.errno = CouponErrorCode.unKnow;
        this.errmsg = "";
    }

    public CouponException(String errmsg) {
        super(errmsg);

        this.errno = CouponErrorCode.unKnow;
        this.errmsg = "";
    }

    /**
     * 构造函数
     *
     * @param errno
     * @param errmsg
     */
    public CouponException(CouponErrorCode errno, String errmsg, Exception e) {
        super(errmsg, e);

        this.errno = errno;
        this.errmsg = errmsg;
    }

    public static CouponException create(CouponErrorCode errno) {
        log.error(errno.toChString());
        return new CouponException(errno);
    }

    public static CouponException create(CouponErrorCode errno, String errmsg, Object... args) {
        return new CouponException(errno, Sb.format(errmsg, args));
    }

    public static CouponException create(Exception e) {
        return new CouponException(e);
    }

    public static CouponException create(String errmsg) {
        return new CouponException(errmsg);
    }

    /**
     * 打印日志
     *
     * @return
     */
    public String printLog() {
        return new StringBuilder().append("DiDi exception, type:").append(this.errno.toChString()).append(" errmsg:").append(this.errmsg)
                .toString();
    }
}
